using System;
using System.Collections.Generic;
using System.Linq;
using MNH.Yarekoodak.DomainClasses;
using MNH.Yarekoodak.DomainClasses.Repositories;

namespace MNH.Yarekoodak.Services.Users
{
    /// <summary>
    /// User service
    /// </summary>
    public partial class UserService : IUserService
    {
        #region Fields
        private readonly IRepository<User> _userRepository = new EfRepository<User>();
        private readonly IRepository<Role> _roleRepository = new EfRepository<Role>();
        #endregion

        #region Ctor
        public UserService()
        {
        }

        #endregion

        #region Methods

        /// <summary>
        /// Deletes a user 
        /// </summary>
        /// <param name="user">User </param>
        public virtual void DeleteUser(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            _userRepository.Delete(user);
        }

        ///// <summary>
        ///// Gets a user 
        ///// </summary>
        ///// <param name="userId">User  identifier</param>
        ///// <returns>User </returns>
        public virtual User GetUserById(int userId, bool asNoTrack = false)
        {
            if (userId == 0)
                return null;

            return _userRepository.Table.Where(c => c.Id == userId).SingleOrDefault();
        }

        ///// <summary>
        ///// Gets a user 
        ///// </summary>
        ///// <param name="userId">User  identifier</param>
        ///// <returns>User </returns>
        public virtual User GetUserByNationalCode(string nationalCode, bool asNoTrack = false)
        {
            return _userRepository.Table.FirstOrDefault(c => c.NationalCode == nationalCode);
        }

        public IEnumerable<User> GetAllUseres()
        {
            var users = _userRepository.Table;
            users = users.OrderByDescending(b => b.Family);
            return users;
        }

        public IEnumerable<User> GetAllUsersByRoleId(int roleId)
        {
            var role = _roleRepository.Table.Where(c => c.Id == roleId).FirstOrDefault();
            if (role == null)
                return null;
            if (role.Type)
            {
                return _userRepository.Table.Where(c => c.GroupUsers.Any(g => g.GroupId == role.UserOrGroupId));
            }
            else
            {
                return _userRepository.Table.Where(c => c.Id == role.UserOrGroupId);
            }
        }


        /// <summary>
        /// Inserts an user 
        /// </summary>
        /// <param name="user">User </param>
        public virtual void InsertUser(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            _userRepository.Insert(user);
        }

        /// <summary>
        /// Updates the user 
        /// </summary>
        /// <param name="user">User </param>
        public virtual void UpdateUser(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            _userRepository.Update(user);
        }


        #endregion



    }
}
